package com.asd.map.controller;

import com.alibaba.fastjson.JSONArray;
import com.asd.common.Result;
import com.asd.map.entity.Line_style;
import com.asd.map.entity.Point_style;
import com.asd.map.entity.Surface_style;
import com.asd.map.entity.vo.Line_styleVO;
import com.asd.map.entity.vo.Surface_styleVO;
import com.asd.map.service.ISurface_styleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @createtime 2021-10-15 9:44
 * @description
 */
@RestController
@RequestMapping("/api/surface_style")
@Api(tags = "面样式Api")
public class Surface_styleController {

    @Autowired
    private ISurface_styleService surface_styleService;


    /**
     * 编辑
     *
     * @param
     * @return
     */
    @ApiOperation(value = "面样式-编辑", notes = "面样式-编辑")
    @PutMapping(value = "/update")
    public Result<?> edit(@RequestBody Surface_style surface_style) {

        List<Surface_style> styleList = surface_styleService.findByApplyId(surface_style.getApplyId(), surface_style.getDid());

        if (styleList.size() == 0) {//不存在面样式时就新增

            surface_styleService.save(surface_style);

            return Result.ok(surface_style);
        } else {

//            for (Surface_style surface_style1 :styleList) {
            for (int i = 0; i < styleList.size(); i++) {
                Surface_style surface_style1 = styleList.get(i);

                if (surface_style1.getType().equals("0")) {//存在样式为统一样式时

                    Surface_style oldSurfaceStyle = surface_styleService.getById(surface_style1.getId());

                    oldSurfaceStyle.setType(surface_style.getType());

                    if (!(surface_style.getStyle().equals(null) || surface_style.getStyle().equals(""))) {

                        oldSurfaceStyle.setStyle(surface_style.getStyle());
                    }
                    if (!(surface_style.getSize().equals(null) || surface_style.getSize().equals(""))) {
                        oldSurfaceStyle.setSize(surface_style.getSize());
                    }
                    if (!(surface_style.getFillColor().equals(null) || surface_style.getFillColor().equals(""))) {
                        oldSurfaceStyle.setFillColor(surface_style.getFillColor());
                    }
                    if (!(surface_style.getFrameColor().equals(null) || surface_style.getFrameColor().equals(""))) {
                        oldSurfaceStyle.setFrameColor(surface_style.getFrameColor());
                    }
                    surface_styleService.updateById(oldSurfaceStyle);

                } else {//存在样式为不是统一样式时
                    surface_styleService.removeById(surface_style1.getId());
                    if (i == styleList.size() - 1){
                        surface_styleService.save(surface_style);
                    }
                }

            }
//

            return Result.ok(surface_style);
        }
    }

    /**
     * 分类样式
     *
     * @param
     * @return
     */
    @ApiOperation(value = "面样式-分类样式", notes = "面样式-分类样式")
    @PutMapping(value = "/updateStyleTriage")
    public Result<?> updateStyleTriage(@RequestBody Surface_styleVO surface_styleVO) {

        List<Surface_style> styleList = surface_styleService.findByApplyId(surface_styleVO.getApplyId(), surface_styleVO.getDid());

        List<Surface_style> list = new ArrayList<>();

        if (styleList.size() > 0) {
            for (Surface_style surface_style : styleList) {
                surface_styleService.removeById(surface_style.getId());
            }
        }

        String data = surface_styleVO.getData();


        List<Map<String, String>> dataList = (List<Map<String, String>>) JSONArray.parse(data);
        for (int i = 0; i < dataList.size(); i++) {
            Map<String, String> map = dataList.get(i);
            String lid = map.get("lid");
            String size = map.get("size");
            String style = map.get("style");
            String frameColor = map.get("frameColor");
            String fillColor = map.get("fillColor");

            Surface_style surface_style = new Surface_style();
            surface_style.setApplyId(surface_styleVO.getApplyId());
            surface_style.setDid(surface_styleVO.getDid());
            surface_style.setLid(lid);
            surface_style.setType(surface_styleVO.getType());
            surface_style.setSize(size);
            surface_style.setStyle(style);
            surface_style.setFrameColor(frameColor);
            surface_style.setFillColor(fillColor);
            surface_styleService.save(surface_style);
            list.add(surface_style);
        }

        return Result.ok(list);

    }


    /**
     * 分类样式-根据id修改面样式修改
     *
     * @param
     * @return
     */
    @ApiOperation(value = "点位样式-分类样式-根据id修改面样式修改", notes = "点位样式-分类样式-根据id修改面样式修改")
    @PutMapping(value = "/updateStyleById")
    public Result<?> updateStyleById(@ApiParam(name = "id", value = "样式ID", required = true) @RequestParam("id") String id,
                                     @ApiParam(name = "size", value = "边框粗细") @RequestParam("size") String size,
                                     @ApiParam(name = "style", value = "边框样式，实线：0；虚线：1") @RequestParam("style") String style,
                                     @ApiParam(name = "frameColor", value = "边框颜色") @RequestParam("frameColor") String frameColor,
                                     @ApiParam(name = "fillColor", value = "填充颜色") @RequestParam("fillColor") String fillColor) {

        Surface_style surfaceStyle = surface_styleService.getById(id);
        if (surfaceStyle == null) {
           return Result.error("修改的样式不存在！");
        } else {
            surfaceStyle.setSize(size);
            surfaceStyle.setStyle(style);
            surfaceStyle.setFrameColor(frameColor);
            surfaceStyle.setFillColor(fillColor);
            surface_styleService.updateById(surfaceStyle);
            return Result.ok(surfaceStyle);
        }

    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "面样式-通过id删除", notes = "面样式-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {

        surface_styleService.removeById(id);


        return Result.ok("删除成功!");
    }

    /**
     * 通过id删除
     *
     * @param
     * @return
     */
    @ApiOperation(value = "面样式-删除数据源关联样式", notes = "面样式-通过id删除")
    @DeleteMapping(value = "/deleteForInformation")
    public Result<?> delete1(@RequestParam(name = "applyId", required = true) String applyId, @RequestParam(name = "did", required = true) String did) {

        surface_styleService.deleteById(applyId, did);


        return Result.ok("删除成功!");
    }


}
